﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 数据库应用程序设计综合
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        OleDbConnection myconn = null;
        //OleDbCommand mycmd = null;
        //OleDbDataReader myreader = null;
        DataSet mydataSet = null;
        OleDbDataAdapter mydaraApter = null;
        string mysql = null;
        public static string mystr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\Access数据库\\21.mdb";
        private void Form2_Load(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            myconn = new OleDbConnection();
            myconn.ConnectionString = mystr;
            myconn.Open();
            mydataSet = new DataSet();

            if (checkBox1.Checked&&textBox1.Text!="")
            {
                //mycmd = new OleDbCommand("select * from tbluser",myconn);
                mysql = "select * from tbluser where 用户名 like '%"+textBox1.Text+"%'";
                mydaraApter = new OleDbDataAdapter(mysql, myconn);
                mydaraApter.Fill(mydataSet, "tbluser");
                dataGridView1.DataSource = mydataSet.Tables["tbluser"];
            }
            else if (!checkBox1.Checked&&textBox1.Text!="")
            {
                //   mycmd = new OleDbCommand();
                mysql = "select * from tbluser where 用户名='" + textBox1.Text+"'";
                //  mycmd.CommandText = mysql;
                //  mycmd.Connection = myconn;
                //  mycmd.Parameters.Add("@name", OleDbType.VarChar, 10).Value = textBox1.Text;
                mydaraApter = new OleDbDataAdapter(mysql, myconn);
                mydaraApter.Fill(mydataSet, "tbluser");
                dataGridView1.DataSource = mydataSet.Tables["tbluser"];
            }
            if (textBox1.Text=="")
            {
                mysql = "select * from theUser";
                mydaraApter = new OleDbDataAdapter(mysql, myconn);
                mydaraApter.Fill(mydataSet, "tbluser");
                dataGridView1.DataSource = mydataSet.Tables["tbluser"];
            }
            myconn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OleDbCommandBuilder mycmdbuilder = new OleDbCommandBuilder(mydaraApter);
            if (mydataSet.HasChanges())             // 如果数据有变动
            {
                try
                {
                    mydaraApter.Update(mydataSet, "tbluser");//更新数据源
                    MessageBox.Show("保存成功","信息提示");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("数据修改不正确","信息提示");
                    
                }
            }
        
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
